package com.zuimei.zuimei.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zuimei.zuimei.R;
import com.zuimei.zuimei.beans.FindAppItem2Bean;
import com.zuimei.zuimei.utils.ImgUtil;
import com.zuimei.zuimei.utils.ScreenUtil;

import java.util.List;

/**
 * Created by Jogger on 2017/1/17.
 * 发现应用--最美
 */

public class Item2RecycleViewItemAdapter extends RecyclerView.Adapter<Item2RecycleViewItemAdapter
        .MyViewHolder> {
    private Context mContext;
    private List<FindAppItem2Bean> mAppList;
    private OnItemClickListener mListener;
    public interface OnItemClickListener{
        void onItemClick(View view, FindAppItem2Bean app);
    }

    public Item2RecycleViewItemAdapter(Context context) {
        mContext = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int
            viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.rv_item2_item, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(Item2RecycleViewItemAdapter.MyViewHolder holder, int position) {
        final FindAppItem2Bean app = mAppList.get(position);
        holder.tv_title.setText(app.getTitle());
        holder.tv_sub_title.setText(app.getSub_title());
        Picasso.with(mContext)
                .load(app.getIcon_image())
                .config(Bitmap.Config.RGB_565)
                .into(holder.iv_head);
        Picasso.with(mContext)
                .load(app.getCover_image())
                .config(Bitmap.Config.RGB_565)
                .transform(ImgUtil.getTransformation(ScreenUtil.getSrceenWidth(mContext)))
                .into(holder.iv_cover_image);
        holder.tv_up_times.setText(app.getUp_times()+"");
        holder.down_times.setText(app.getDown_times()+"");
        holder.tv_comment_times.setText(app.getComments().size()+"");
        holder.tv_author_name.setText(app.getAuthor_name());
        Picasso.with(mContext)
                .load(app.getAuthor_avatar_url())
                .config(Bitmap.Config.RGB_565)
                .into(holder.iv_author_avatar_url);
        holder.ll_content_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mListener!=null){
                    mListener.onItemClick(view,app);
                }
            }
        });
    }

    public void loadData(List<FindAppItem2Bean> appList) {
        mAppList = appList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mAppList.size();
    }
    public void setOnItemClickListener(OnItemClickListener listener){
        mListener=listener;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        LinearLayout ll_content_container;
        TextView tv_title;
        ImageView iv_head;
        TextView tv_sub_title;
        ImageView iv_cover_image;
        TextView tv_up_times;
        TextView down_times;
        TextView tv_comment_times;
        TextView tv_author_name;
        ImageView iv_author_avatar_url;

        MyViewHolder(View itemView) {
            super(itemView);
            ll_content_container= (LinearLayout) itemView.findViewById(R.id.ll_content_container);
            tv_title = (TextView) itemView.findViewById(R.id.tv_title);
            iv_head = (ImageView) itemView.findViewById(R.id.iv_head);
            tv_sub_title = (TextView) itemView.findViewById(R.id.tv_sub_title);
            iv_cover_image = (ImageView) itemView.findViewById(R.id.iv_cover_image);
            tv_up_times = (TextView) itemView.findViewById(R.id.tv_up_times);
            down_times = (TextView) itemView.findViewById(R.id.down_times);
            tv_comment_times = (TextView) itemView.findViewById(R.id.tv_comment_times);
            tv_author_name = (TextView) itemView.findViewById(R.id.tv_author_name);
            iv_author_avatar_url = (ImageView) itemView.findViewById(R.id.iv_author_avatar_url);
        }
    }
}
